<template>
  <div class="post-comment">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="优质评论将会被优先展示"
      show-word-limit
    />
    <van-button
      class="post-btn"
      size="small"
      @click="onAddComment"
    >发布</van-button>
  </div>
</template>

<script>
import { addCommentAPI } from '../../../api/index.js'
export default {
  name: 'CommentPost',
  props: {
    targetId: {
      type: String,
      default: ''
    },
    artId: {
      type: String,
      default: null
    }
  },
  data () {
    return {
      message: ''
    }
  },
  methods: {
    // 发布评论
    async onAddComment () {
      try {
        // 1. 判空验证
        if (this.message.trim().length === 0) return

        // 2. 请求添加接口
        const { data: res } = await addCommentAPI({
          target: this.targetId, // 评论的目标id（评论文章即为文章id，对评论进行回复则为评论id）
          content: this.message,
          art_id: this.artId // 文章id （注意事项： target为评论id时，才传该属性）
        })
        console.log(res)

        // 3. 情况输入框
        this.message = ''

        // 4. 发布成功后，关闭弹框
        this.$emit('postSuccess', res.data.new_obj)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="less">
.post-comment {
  display: flex;
  align-items: center;
  padding: 16px 0 16px 16px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 75px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
